
module.exports = class extends baseController{
    constructor(){
        super();
        this.partnersDictLogic=this.loadLogic('iframe/partners-dict');
        this.router.post('/getItems',this.getItems.bind(this));
        this.router.post('/editModule',this.editModule.bind(this));
        this.router.post('/delModule',this.delModule.bind(this));
        this.router.post('/moveNode',this.moveNode.bind(this));
        this.router.post('/asycDict',this.asycDict.bind(this));
        this.router.post('/getdictsTreeData',this.getdictsTreeData.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let Result={};
        try{
            let queryModel={
                label:req.query.label ? req.query.label : '',
                PartnersID:req.body.partnersID? Number(req.body.partnersID) : Number(req.user.UserPartnersID)
            }
            let ret = await this.partnersDictLogic.getItems(queryModel);
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error("数据操作出错",err);
        }
        res.json(Result);
    }

    async editModule(req,res){
        let Result={};
        try{
            let ret = await this.partnersDictLogic.insertOrUpdate(req.body);
            if(ret){
                Result=this.Success(1,ret);
            }else{
                Result=this.Error('记录不存在');
            }
           
        }catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async delModule(req,res){
        let Result={};
        try{
            let AllIDs = req.body.AllIDs;
        let CurNodeID = req.body.CurNodeID;
        let CurNodeParentID = req.body.CurNodeParentID;
        let AllIDs_Array = AllIDs.split(',');
            await this.partnersDictLogic.delModule(CurNodeParentID,AllIDs_Array);
            Result=this.Success(1);
        }catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async moveNode(req,res){
        let Result={};
        try{
            let TargetNodeChildrensIDs=req.body["TargetNodeChildrensIDs"] + ",";
            let queryModel={
                CurNodeID:Number("0" + req.body["CurNodeID"]),
                TargetParentID: Number("0" + req.body["TargetParentID"]),
                old_parent: Number("0" + req.body["old_parent"]),
                TargetNodeChildrensIDs: TargetNodeChildrensIDs,
                TargetNodeChildrensIDs_array:TargetNodeChildrensIDs.split(',')
            }
            let ret = await this.partnersDictLogic.moveNode(queryModel);
            if(ret){
                Result=this.Success(1);
            }else{
                Result=this.Error('操作失败');
            }
           
        }catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async asycDict(req,res){
        let Result={};
        try{
            
            let ret = await this.partnersDictLogic.asycDict();
            if(ret){
                Result=this.Success(1,ret);
            }else{
                Result=this.Error('操作失败');
            }
           
        }catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    /**
     * 获取字典表的treeData 给组件用
     * @param {*} req 
     * @param {*} res 
     */
    async getdictsTreeData(req,res){
        let Result={};
        try{
            let queryModel={
                label:req.body.label ||  '',
                parentId:req.body.parentId || '',
                partnersId:req.body.partnersId ? req.body.partnersId : ''
            };
            let ret = await this.partnersDictLogic.getdictsTreeData(queryModel);
            if(ret){
                Result=this.Success(1,ret);
            }else{
                Result=this.Error('操作失败');
            }
           
        }catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }


};